﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using PagedList;
using DLL;
using CommonHelpers;

namespace QuanLyBanLe.Sell
{
    public partial class FormReturnProductHistory : Form
    {
        private ReturnProductService m_Service;
        private List<pSearchReturnProduct_Result> m_ReturnProducts;
        private int m_PageNumber;
        private int m_PageSize;
        private int m_MaxPage;
        private int m_TotalRecord;
        private bool m_EnabledEvent;
        private int m_TotalPriceReturn;

        public FormReturnProductHistory()
        {
            InitializeComponent();
            m_Service = new ReturnProductService();
            m_ReturnProducts = new List<pSearchReturnProduct_Result>();
            m_EnabledEvent = false;
            m_PageSize = 20;
            m_PageNumber = 1;
            m_TotalPriceReturn = 0;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            BindDataSource();
        }

        private void FormReturnProductHistory_Load(object sender, EventArgs e)
        {
            dtpFromDate.Value = DateTime.Now;
            dtpToDate.Value = DateTime.Now;
            dtgReturnProduct.AutoGenerateColumns = false;
            dtgReturnProductDetail.AutoGenerateColumns = false;
            lblTotalPrice.Text = "";
            BindDataSource();
            this.Text += FormMain.GetShopInfo();
        }

        private void dtgReturnProduct_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (m_EnabledEvent)
            {
                int returnProductId = Convert.ToInt32(dtgReturnProduct.Rows[e.RowIndex].Cells["colId"].Value);
                if (returnProductId > 0)
                {
                    var details = m_Service.GetDetailById(returnProductId);
                    dtgReturnProductDetail.DataSource = details;
                }
            }
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            m_PageNumber = 1;
            ChangePage();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_PageNumber > 1 ? m_PageNumber - 1 : m_PageNumber;
            ChangePage();
        }

        private void mnrPageIndex_ValueChanged(object sender, EventArgs e)
        {
            if (m_EnabledEvent)
            {
                m_PageNumber = (int)mnrPageIndex.Value;
                ChangePage();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_PageNumber < m_MaxPage ? m_PageNumber + 1 : m_PageNumber;
            ChangePage();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_MaxPage;
            ChangePage();
        }

        private void SetEnabledForPagingNavigator()
        {
            if (m_PageNumber == m_MaxPage)
            {
                if (m_PageNumber == 1)
                {
                    btnFirst.Enabled = false;
                    btnPrev.Enabled = false;
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
                else
                {
                    btnFirst.Enabled = true;
                    btnPrev.Enabled = true;
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
            }
            else if (m_PageNumber == 1)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                btnLast.Enabled = true;
                btnNext.Enabled = true;
            }
            else
            {
                btnFirst.Enabled = true;
                btnPrev.Enabled = true;
                btnLast.Enabled = true;
                btnNext.Enabled = true;
            }
        }

        private void ChangePage()
        {
            m_EnabledEvent = false;
            IPagedList<pSearchReturnProduct_Result> pagedList = m_ReturnProducts.ToPagedList(m_PageNumber, m_PageSize);
            dtgReturnProduct.DataSource = pagedList.ToList();

            // Prevent changePage call second time from mnrPageIndex_ValueChanged           
            mnrPageIndex.Value = m_PageNumber;
            m_EnabledEvent = true;

            m_TotalRecord = pagedList.TotalItemCount;
            m_MaxPage = pagedList.PageCount;
            lblTotalPage.Text = "/" + m_MaxPage.ToString();

            SetEnabledForPagingNavigator();
        }

        private void GetData()
        {
            if (m_PageNumber < 1)
            {
                m_PageNumber = 1;
            }

            m_ReturnProducts = m_Service.Search(dtpFromDate.Value, dtpToDate.Value);
            m_TotalPriceReturn = 0;
            foreach (pSearchReturnProduct_Result item in m_ReturnProducts)
            {
                m_TotalPriceReturn += item.TotalPrice.Value;
            }
        }

        private void BindDataSource()
        {
            m_EnabledEvent = false;
            GetData();
            IPagedList<pSearchReturnProduct_Result> pagedList = m_ReturnProducts.ToPagedList(m_PageNumber, m_PageSize);
            dtgReturnProduct.DataSource = pagedList.ToList();
            m_TotalRecord = pagedList.TotalItemCount;
            m_MaxPage = pagedList.PageCount;
            lblTotalPrice.Text = m_TotalPriceReturn.ToMoney();
            if (m_TotalRecord > 0)
            {
                lblTotalPage.Text = "/" + m_MaxPage.ToString();
                m_EnabledEvent = false;
                mnrPageIndex.Minimum = 1;
                mnrPageIndex.Enabled = true;
                mnrPageIndex.Maximum = m_MaxPage;
                mnrPageIndex.Value = m_PageNumber;
                SetEnabledForPagingNavigator();
            }
            else
            {
                m_PageNumber = 0;
                mnrPageIndex.Minimum = 0;
                mnrPageIndex.Value = 0;
                mnrPageIndex.Enabled = false;
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                btnLast.Enabled = false;
                btnNext.Enabled = false;
            }
            m_EnabledEvent = true;
        }
    }
}
